package com.ly.dao.impl;



import com.ly.dao.PersonDao;
import com.ly.entity.Person;
import com.ly.entity.PersonDTO;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Repository
@Transactional
public class PersonDaoImpl implements PersonDao {
    @Autowired
    private SessionFactory sessionFactory;
    public void setSessionFactory(SessionFactory sessionFactory){
        this.sessionFactory = sessionFactory;
    }
    public Person getPerson(String id) {
        String hql = "from Person p where p.id=?";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        query.setString(0, id);

        return (Person)query.uniqueResult();
    }

    public List<Person> getAllPerson() {
        String hql = "from Person";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        return query.list();
    }

    public void get(){
        String hql = "select name from Person where id = ?";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        query.setString(0,"1");
        List<Object> list = query.list();

        for(Object obj : list){
            System.out.println("name:"+obj);
        }
    }

    @Override
    public void get1() {
        String hql = "select name,email from Person where id = ?";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        query.setString(0,"1");
        List<Object[]> list= query.list();

        for(Object[] objs : list){
            System.out.println("========name:"+objs[0]);
            System.out.println("========email:"+objs[1]);
        }
    }


    /**
     *
     * 复杂查询返回对象的情况
     * @return
     */
    @Override
    public List<PersonDTO> get2() {
        String  sql="select name,email,childhood_name as childhoodName  from Person";
        Query query = sessionFactory.getCurrentSession().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(PersonDTO.class));
        List<PersonDTO> list= query.list();

        return  list;
    }

    @Override
    public void get3() {

    }

    public void addPerson(Person person) {
        sessionFactory.getCurrentSession().save(person);
    }

    public boolean delPerson(String id) {
        String hql = "delete Person p where p.id = ?";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        query.setString(0, id);
        return (query.executeUpdate() > 0);
    }

    public boolean updatePerson(Person person) {
        String hql = "update Person p set p.name = ? where p.id = ?";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        query.setString(0, person.getName());
        query.setString(1, String.valueOf(person.getId()));
        return (query.executeUpdate() > 0);
    }



}